package cn.huaguo.trigger.http;

import cn.huaguo.domain.campus.model.entity.CampusEntity;
import cn.huaguo.domain.campus.model.valobj.CampusVO;
import cn.huaguo.domain.campus.service.ICampusService;
import cn.huaguo.trigger.http.dto.CampusAddReqDTO;
import cn.huaguo.types.enums.ResponseCode;
import cn.huaguo.types.model.Response;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description CampusController
 * @Author wangQian
 * @Date 2024/5/15
 */
@Slf4j
@RestController
@RequestMapping("/campus")
public class CampusController {
    @Resource
    private ICampusService campusService;

    /**
     * 新增校区
     * @param campusAddReqDTO
     * @return
     */
    @PostMapping("/addCampus")
    public Response addCampus(@RequestBody CampusAddReqDTO campusAddReqDTO) {
        try {
            log.info("新增校区：{}", JSON.toJSONString(campusAddReqDTO));
            campusService.addCampus(CampusVO.builder()
                            .campusName(campusAddReqDTO.getCampusName())
                            .campusAddress(campusAddReqDTO.getCampusAddress())
                            .build());
            log.info("新增校区成功");

            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();

        } catch (Exception e) {
            log.error("新增校区失败：{}", e.getMessage());
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 查询校区列表
     * @return
     */
    @GetMapping("/campusList")
    public Response<List<CampusEntity>> campusList() {
        try {
            log.info("查询校区列表");
            List<CampusEntity> campusEntities = campusService.listAllCampus();

            log.info("查询校区列表成功");
            return Response.<List<CampusEntity>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(campusEntities)
                    .build();
        } catch (Exception e) {
            log.error("查询校区列表失败：{}", e.getMessage());
            return Response.<List<CampusEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .data(null)
                    .build();
        }
    }

    @GetMapping("/getById/{id}")
    public Response<CampusEntity> getCampusById(@PathVariable("id") Integer id) {
        try {
            log.info("根据id查询校区 id：{}", id);
            CampusEntity campusEntity = campusService.getCampusById(id);

            log.info("根据id查询校区成功：{}", JSON.toJSONString(campusEntity));

            return Response.<CampusEntity>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(campusEntity)
                    .build();
        } catch (Exception e) {
            log.error("根据id查询校区失败 id：{}", e.getMessage());
            return Response.<CampusEntity>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .data(null)
                    .build();
        }
    }

    /**
     * 更新校区byId
     * @param campusEntity
     * @return
     */
    @PutMapping("/updateCampus")
    public Response<Boolean> updateCampus(@RequestBody CampusEntity campusEntity) {
        try {
            log.info("更新校区：{}", JSON.toJSONString(campusEntity));
            Boolean result = campusService.updateCampus(campusEntity);

            log.info("更新校区成功：{}", result);
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(result)
                    .build();
        } catch (Exception e) {
            log.error("更新校区失败：{}", e.getMessage());
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .data(false)
                    .build();
        }
    }

    /**
     * 删除校区byId
     * @param campusId
     * @return
     */
    @DeleteMapping("/{campusId}")
    public Response deleteCampus(@PathVariable("campusId") Integer campusId) {
        try {
            log.info("删除校区 id：{}", campusId);
            campusService.deleteCampus(campusId);

            log.info("删除校区成功");
            return Response.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("删除校区失败 id：{}", e.getMessage());
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(StringUtils.hasLength(e.getMessage()) ? e.getMessage() : ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
}
